// MUI.cpp: implementation of the CMUI class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansieditor.h"
#include "MUI.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

char CMUI::szEntry[12] = { 0 };
CString CMUI::m_pszProfileName;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMUI::CMUI()
{
	m_bNoOp = TRUE;
	m_nCP = GetOEMCP();
}

CMUI::~CMUI()
{

}

void CMUI::SetCP(UINT nCP/* = 0 */)
{
	if(!nCP)
	{
		nCP = 437; // English
	}
	CString sOcp;
	switch(nCP)
	{
	case 936:
		sOcp = "chs";
		break;
	case 950:
		sOcp = "cht";
		break;
	case 932:
		sOcp = "jpn";
		break;
	default:
		sOcp = "eng";
		break;
	}
	m_pszProfileName.Format("%s\\language\\ae%s.ini", gpApp->GetAppPath(), sOcp);
	m_bNoOp = (nCP == 936 && nCP == m_nCP);
	if(!FileExists(m_pszProfileName))
	{
		m_nCP = 936;
		m_bNoOp = TRUE;
	}
	m_nCP = nCP;
}

void CMUI::UpdateMenu(CMenu *pMenu)
{
	if(m_bNoOp)
	{
		return;
	}
	int nCount = pMenu->GetMenuItemCount();
	MENUITEMINFO mi = { 0 };
	mi.cbSize = sizeof(mi);
	mi.fMask = MIIM_FTYPE|MIIM_ID|MIIM_SUBMENU;

	CPlugin *pPlugin;
	CString sEntry, sText;
	for(int n = 0; n < nCount; n++)
	{
		if(!pMenu->GetMenuItemInfo(n, &mi, TRUE))
		{
			continue;
		}
		if(mi.fType & MF_SEPARATOR)
		{
			continue;
		}
		if(!(mi.fType & MFT_SEPARATOR))
		{
			pPlugin = gpPluginManager->GetPluginByCommandID(mi.wID);
			if(pPlugin)
			{
				
				pMenu->ModifyMenu(n, MF_BYPOSITION|MF_STRING, mi.wID, pPlugin->GetMenuName());
			}
			else if(mi.hSubMenu)
			{
				UpdateMenu(CMenu::FromHandle(mi.hSubMenu));
				MENUITEMINFO miSub = { 0 };
				miSub.cbSize = sizeof(miSub);
				miSub.fMask = MIIM_ID;
				::GetMenuItemInfo(mi.hSubMenu, 0, TRUE, &miSub);
				sEntry.Format("%up", miSub.wID);
				if(GetIniStr("Menu", sEntry, sText))
				{
					pMenu->ModifyMenu(n, MF_BYPOSITION|MF_POPUP, (UINT)mi.hSubMenu, sText);
				}
			}
			else
			{
				sEntry.Format("%u", mi.wID);
				if(GetIniStr("Menu", sEntry, sText))
				{
					pMenu->ModifyMenu(n, MF_BYPOSITION|MF_STRING, mi.wID, sText);
				}
			}
		}
	}
}

BOOL CMUI::GetIniStr(LPCTSTR szSection, LPCTSTR szEntry, CString &sResult, LPCTSTR szProfileName/* = m_pszProfileName */)
{
	TCHAR szT[4096];
	DWORD dw = ::GetPrivateProfileString(szSection, szEntry, &afxChNil, szT, 4096, szProfileName);
	ASSERT(dw < 4095);
	if(dw == 0)
	{
		return FALSE;
	}
	sResult = szT;
	return TRUE;
}

CString CMUI::LoadString(UINT nID)
{
	CString sResult;
	if(m_bNoOp)
	{
		sResult.LoadString(nID);
	}
	else
	{
		static char szEntry[12];
		ultoa(nID, szEntry, 10);
		if(!GetIniStr("String", szEntry, sResult))
		{
			sResult.LoadString(nID);
		}
	}
	sResult.Replace('|', '\n');
	return sResult;
}

int CMUI::GetIniStr(LPCTSTR szSection, LPCTSTR szEntry, LPTSTR lpszBuf, UINT nMaxBuf/* = 256*/)
{
	int nLen = GetPrivateProfileString(szSection, szEntry, &afxChNil, lpszBuf, nMaxBuf, m_pszProfileName);
	/*for(int n = 0; n < nLen; n++)
	{
		if(lpszBuf[n] == '|')
		{
			lpszBuf[n] = '\n';
//			break;
		}
	}*/
	return nLen;
}

int CMUI::LoadString(UINT nIDS, LPTSTR lpszBuf, UINT nMaxBuf/* = 256 */)
{
	int nResult = 0;
	if(m_bNoOp)
	{
		nResult = AfxLoadString(nIDS, lpszBuf, nMaxBuf);
	}
	else
	{
		ultoa(nIDS, szEntry, 10);
		nResult = GetIniStr("String", szEntry, lpszBuf, nMaxBuf);
		if(nResult == 0)
		{
			nResult = AfxLoadString(nIDS, lpszBuf, nMaxBuf);
		}
	}
	return nResult;
}

void CMUI::UpdateDialog(CDialog *pDialog, UINT IDD)
{
	if(m_bNoOp)
	{
		return;
	}
	HWND hParentWnd = pDialog->GetSafeHwnd();
	HWND hWnd = ::FindWindowEx(hParentWnd, NULL, NULL, NULL);
	CString sText;
	CString sSection;
	sSection.Format("Dlg_%u", IDD);
	
	while(hWnd)
	{
		UINT nID = GetDlgCtrlID(hWnd);
		ultoa(nID, szEntry, 10);
		if(GetIniStr(sSection, szEntry, sText) || 
		   GetIniStr("Dialog_Common", szEntry, sText))
		{
			SetDlgItemText(hParentWnd, nID, sText);
		}

		hWnd = ::FindWindowEx(hParentWnd, hWnd, NULL, NULL);
	}
	if(GetIniStr(sSection, "Title", sText))
	{
		pDialog->SetWindowText(sText);
	}
}

void CMUI::OnLanguageMenuInitPopup(CMenu *pMenu)
{
	int nCount = pMenu->GetMenuItemCount();
	for(int n = 1; n < nCount; n++)
	{
		pMenu->DeleteMenu(1, MF_BYPOSITION);
	}

	CString sLanguage;
	CString sCP;
	CString sIniFile;
	m_arrLangCP.RemoveAll();

	WIN32_FIND_DATA fd;
	CString &sAppPath = gpApp->GetAppPath();
	HANDLE hFind = FindFirstFile(sAppPath + "\\language\\*.ini", &fd);
	n = 0;
	if(hFind != INVALID_HANDLE_VALUE)
	{
		pMenu->AppendMenu(MF_SEPARATOR, 0);
		do
		{
			if(fd.cFileName[0] == '.')
			{
				continue;
			}
			sIniFile.Format("%s\\language\\%s", sAppPath, fd.cFileName);
			GetIniStr("Info", "language", sLanguage, sIniFile);
			pMenu->AppendMenu(MF_STRING, IDM_LANG_BASE + n, sLanguage);
			sCP = "437";
			GetIniStr("Info", "codepage", sCP, sIniFile);
			m_arrLangCP.Add(atol(sCP));
			++n;
			
		} while(FindNextFile(hFind, &fd));
	}
	FindClose(hFind);
}

UINT CMUI::GetMenuCP(UINT nID)
{
	int nIndex = nID - IDM_LANG_BASE;
	if( nIndex < 0 || nIndex >= m_arrLangCP.GetSize())
	{
		return 0;
	}
	return m_arrLangCP.GetAt(nIndex);
}

UINT CMUI::GetCurrentCP()
{
	return m_nCP;
}

BOOL CMUI::IsDefaultCP()
{
	return m_nCP == 936;
}